<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 用户列表 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="page.name" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-circle-plus-outline" @click="OpenuserRole()">赋予角色</el-button>
            </div>
            <el-tree
                :data="data"
                style="width: 10%; float: left"
                :default-expand-all="true"
                :props="defaultProps"
                @node-click="handleNodeClick"
            ></el-tree>
            <el-table
                :data="tableData"
                border
                class="table"
                style="width: 85%; float: right"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="login" label="用户名"></el-table-column>
                <!-- <el-table-column label="账户余额">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column> -->
                <el-table-column label="头像(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-UserImg"
                            :src="Img + scope.row.userImg"
                            :preview-src-list="[Img + scope.row.userImg]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="角色">
                    <template slot-scope="aaa">
                        <el-tag type="success" v-for="(item, index) in aaa.row.roleNames" :key="index">{{ item.roleName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="phone" label="手机号"></el-table-column>
                <el-table-column prop="emali" label="邮箱"></el-table-column>
                <el-table-column prop="createTime" label="注册时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination" style="clear: both">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="page.pageindex"
                    :page-size="page.pagesize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="昵称">
                    <el-input v-model="form.UserName"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="form.Phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.Emali"></el-input>
                </el-form-item>
                <el-form-item label="上传头像">
                    <el-upload action="http://localhost:5069/api/User/PicLoad" list-type="picture-card"> </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 赋予角色 -->
        <el-dialog title="角色" :visible.sync="endowrole" width="30%">
            <el-table
                :data="tableRole"
                border
                class="table"
                ref="UseRole"
                header-cell-class-name="table-header"
                @selection-change="SelectionChangerole"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID"></el-table-column>
                <el-table-column prop="roleName" label="角色名称"></el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button @click="endowrole = false">取 消</el-button>
                <el-button type="primary" @click="UserRoleAdd()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
//导入getUserList方法
import { getUserList, GetUserInfoById, UrseRole, DeleUserInfo } from '@/api/users.js/';
import { RoleListAll, RoleListById } from '@/api/roles.js';
import { DepartmentgetData } from '@/api/depart.js/';
import Tabs from '../../components/page/Tabs.vue';

export default {
    components: { Tabs },
    // name: 'basetable',
    data() {
        return {
            //地图基地址
            Img: 'http://localhost:5069/Image/',
            //用户列表查询
            page: {
                name: '',
                pageindex: 1,
                pagesize: 2
            },
            tableData: [], //用户信息集合
            tableRole: [], //角色表
            RoleName: [], //角色名称集合
            UseRole: [],
            //复选框
            multipleSelection: [],
            handleSelectionChangerole: [],
            delList: [],
            editVisible: false,
            endowrole: false,
            pageTotal: 0,
            form: {
                UserName: '',
                Phone: '',
                Emali: ''
            },
            idx: -1,
            id: -1,
            RoleUser: {
                RoleId: undefined,
                UserId: undefined
            },
            UserId: undefined,
            RoleId: undefined,
            data: [],
            defaultProps: {
                value: 'id',
                label: 'name',
                children: 'children'
            }
        };
    },
    created() {
        this.getData();
        this.handleNodeClick();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            getUserList(this.page).then((res) => {
                console.log(res);
                this.tableData = res.data.plist;
                /* this.RoleName = res.data.plist[0].roleNames;
                console.log(this.RoleName); */
                this.pageTotal = res.data.recordCount;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    DeleUserInfo(row.id).then((res) => {
                        if (res.code == 200) {
                            this.$message.success('删除成功');
                            this.getUserList();
                        } else {
                            this.$message.success('删除失败');
                        }
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
            val.forEach((x) => {
                this.UserId = x.id;
                this.RoleId = x.roleId;
            });
            console.log(this.multipleSelection);
        },
        SelectionChangerole(val) {
            this.UseRole = val;
            console.log(this.UseRole);
        },
        // 编辑操作
        handleEdit(index, row) {
            let obj = {
                id: row.id
            };
            GetUserInfoById(obj).then((res) => {
                this.form = res;
                this.editVisible = true;
                console.log(this.form);
            });
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改成功`);
            this.$set(this.tableData, this.id, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.page, 'pageindex', val);
            this.getData();
        },
        //角色关系的添加
        OpenuserRole() {
            if (this.multipleSelection == undefined || this.multipleSelection.length == 0 || this.multipleSelection.length > 1) {
                this.$message({
                    message: '请选择一个用户'
                });
                return;
            } else {
                this.RoleShow();
                this.endowrole = true;
                this.RoleListById();
            }
        },
        RoleListById() {
            RoleListById(this.RoleId).then((res) => {
                this.UseRole.push(res);
                console.log(this.UseRole);
            });
        },
        //角色的显示
        RoleShow() {
            RoleListAll().then((res) => {
                this.tableRole = res;
            });
        },
        //用户角色的添加
        UserRoleAdd() {
            let arr = [];
            this.UseRole.forEach((x) => {
                arr.push(x.id);
            });

            let obj = {
                RoleId: arr,
                UserId: this.UserId
            };
            UrseRole(obj).then((res) => {
                if (res.code == 200) {
                    this.$message({
                        message: '添加成功',
                        type: 'success'
                    });
                    this.endowrole = true;
                } else {
                    this.$message({
                        message: '添加失败',
                        type: 'warning'
                    });
                }
                this.endowrole = false;
                this.getData();
            });
        },
        //部门
        handleNodeClick() {
            DepartmentgetData().then((res) => {
                this.data = res.data;
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-UserImg {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
